package com.example.exercise.sys.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.ArrayList;
import java.util.List;

import com.example.exercise.sys.vo.SysRoleMenuVO;
import com.example.exercise.sys.dao.entity.SysRoleMenuEntity;
import com.example.exercise.sys.dao.SysRoleMenuDAO;

/**
* 角色-菜单-对照表
*/
@Service
public class SysRoleMenuService {

    @Autowired
    private SysRoleMenuDAO sysRoleMenuDAO;

    public List<SysRoleMenuVO> findByWhere(Page<?> page, SysRoleMenuVO sysRoleMenuVO){
        List<SysRoleMenuVO> list = new ArrayList<>();
        List<SysRoleMenuEntity> list0 = sysRoleMenuDAO.findByWhere(page, sysRoleMenuVO);
        for (SysRoleMenuEntity sysRoleMenuEntity : list0) {
            list.add(toSysRoleMenuVO(sysRoleMenuEntity));
        }
        return list;
    }

    public SysRoleMenuVO findByKey(SysRoleMenuVO sysRoleMenuVO){
        SysRoleMenuEntity sysRoleMenuEntity = sysRoleMenuDAO.findByKey(sysRoleMenuVO);
        return toSysRoleMenuVO(sysRoleMenuEntity);
    }

    public int insert(SysRoleMenuVO sysRoleMenuVO){
        return sysRoleMenuDAO.insert(toSysRoleMenuEntity(sysRoleMenuVO));
    }

    public int updateByKey(SysRoleMenuVO sysRoleMenuVO){
        return sysRoleMenuDAO.updateByKey(toSysRoleMenuEntity(sysRoleMenuVO));
    }

    public int deleteByKey(SysRoleMenuVO sysRoleMenuVO){
        return sysRoleMenuDAO.deleteByKey(sysRoleMenuVO);
    }

    private SysRoleMenuVO toSysRoleMenuVO(SysRoleMenuEntity sysRoleMenuEntity) {
        SysRoleMenuVO sysRoleMenuVO = new SysRoleMenuVO();
                sysRoleMenuVO.setId(String.valueOf(sysRoleMenuEntity.getId()));
        sysRoleMenuVO.setRoleId(String.valueOf(sysRoleMenuEntity.getRoleId()));
        sysRoleMenuVO.setMenuId(sysRoleMenuEntity.getMenuId());

        return sysRoleMenuVO;
    }

    private SysRoleMenuEntity toSysRoleMenuEntity(SysRoleMenuVO sysRoleMenuVO) {
        SysRoleMenuEntity sysRoleMenuEntity = new SysRoleMenuEntity();
                String id = sysRoleMenuVO.getId();
        if(id != null) {
            sysRoleMenuEntity.setId(Integer.parseInt(sysRoleMenuVO.getId()));
        }
        String roleId = sysRoleMenuVO.getRoleId();
        if(roleId != null) {
            sysRoleMenuEntity.setRoleId(Integer.parseInt(sysRoleMenuVO.getRoleId()));
        }
        sysRoleMenuEntity.setMenuId(sysRoleMenuVO.getMenuId());

        return sysRoleMenuEntity;
    }

}